package com.gzist.elderlyserviceplatform.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.base.BaseDTO;
import com.gzist.elderlyserviceplatform.entity.dto.ListQueryPageDTO;
import com.gzist.elderlyserviceplatform.entity.po.NursingHome;
import com.gzist.elderlyserviceplatform.mapper.NursingHomeMapper;
import com.gzist.elderlyserviceplatform.service.NursingHomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 养老院信息表 服务实现类
 */
@Service
public class NursingHomeServiceImpl extends ServiceImpl<NursingHomeMapper, NursingHome> implements NursingHomeService {
    @Override
    public Page<NursingHome> list(ListQueryPageDTO listQueryPageDTO) {
        LambdaQueryWrapper<NursingHome> wrapper = Wrappers.lambdaQuery();
        if (listQueryPageDTO.getKeyWord() != null) {
            wrapper.like(NursingHome::getName, listQueryPageDTO.getKeyWord());
        }
        return this.page(new Page<>(listQueryPageDTO.getPageNum(), listQueryPageDTO.getPageSize()), wrapper);
    }

    /*
        传递养老院类，根据id修改其他值
     */
    @Override
    public void update(NursingHome nursingHome) {
        LambdaUpdateWrapper<NursingHome> wrapper = new UpdateWrapper<NursingHome>().lambda();
        wrapper.eq(NursingHome::getId, nursingHome.getId());
        Boolean b = this.update(nursingHome, wrapper);
        if (!b) {
            throw new RuntimeException("修改失败");
        }

    }

    @Override
    public Page<NursingHome> page(BaseDTO baseDTO){
        return this.page(new Page<NursingHome>(baseDTO.getPageNum(), baseDTO.getPageSize()));
    }
}
